<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="handleFilter"
        @search-reset="handleRefreshChange"
        @refresh-change="handleRefreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-save="create"
        @row-update="update"
      >
        <template slot="menu" slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-view"
            @click="$refs.crud.rowEdit(scope.row, scope.index)"
            >编辑
          </el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="deletes(scope.row, scope.index)"
            >删除
          </el-button>
        </template>
        <template slot="menuLeft">
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-plus"
            @click="$refs.crud.rowAdd()"
            >添加
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, putBanner, deleteBanner } from "@/api/admin/banner";
import { manageOption } from "@/const/crud/admin/banner";
import { mapGetters } from "vuex";
import { fetchMyTask } from "@/api/admin/task";

import moment from "moment";

export default {
  name: "client",
  data() {
    return {
      tableData: [],
      tableOption: manageOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
    };
  },
  watch: {
  },
  mounted() {
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          params
        )
      ).then(response => {
        this.tableData = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.tableLoading = false;
      });
    },
    fetchTask(){
      fetchMyTask().then(res=>{
        this.myTask = res.data.data
      })
    },
    handleview(data, index) {
      this.detailVisible = true;
      this.detailForm = data;
      getLogs({ batch_id: data.batch_id }).then(res => {
        this.detailTableList = res.data.data;
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    handleFilter(param, done) {
      this.page.currentPage = 1;
      const qDate = {}
      if(this.searchDate){
        qDate.start = moment(this.searchDate[0]).format("YYYY-MM-DD HH:mm:ss")
        qDate.end = moment(this.searchDate[1]).format("YYYY-MM-DD HH:mm:ss")
      }
      this.getList(this.page, Object.assign(param, qDate));
      done();
    },
    handleRefreshChange() {
      this.searchDate = ''
      this.getList(this.page);
    },
    showDownloadDialog() {
      try {
        this.$refs.formdownload.resetForm();
      }catch(e){}
      this.downloadVisible = true;
    },
    showUploadDialog() {
      try {
        this.$refs.formUpload.resetForm()
      }catch(e){}
      this.uploadVisible = true;
    },
    create(row, done, loading) {
      putBanner(row)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("创建成功");
        })
        .catch(() => {
          loading();
        });
    },
    update(row, index, done, loading) {
      putBanner(row)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("修改成功");
        })
        .catch(() => {
          loading();
        });
    },
    deletes(row) {
      this.$confirm("此操作将永久删除该图片, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteBanner({ id: row.id })
          .then(() => {
            this.getList(this.page);
            this.$notify.success("删除成功");
          })
          .catch(() => {
            this.$notify.error("删除失败");
          });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.product_list {
  display: flex;
  flex-direction: column;
  flex: 1;
  .el-radio {
    border: 1px solid #ccc;
    border-radius: 5px;
    padding: 10px;
    margin-bottom: 10px;
    margin-right: 0;
  }
}
.product_item {
  display: flex;
  padding: 0 10px;
  .label {
    width: 120px;
    text-align: right;
    padding-right: 12px;
  }
}
.detailTxt {
  color: #606266;
  height: 32px;
  line-height: 32px;
  padding: 0 15px;
}
</style>
<style lang="scss">
.printCell .cell {
  height: 23px;
}
</style>
